from tools.db import *
import numpy as np
import matplotlib.pyplot as plt
from pyecharts import Line
import os

'''数据分析、机器学习、人工智能相关职位一个月内发布情况'''
def pubtime(keywords):
    a = '%' + keywords[0] + '%'
    b = '%' + keywords[1] + '%'
    c = '%' + keywords[2] + '%'
    Post = lagou_post_info

    mydata = Post.select(Post.publish_time, fn.COUNT(Post.id).alias('num')).where(Post.post_name % a| Post.post_name % b| Post.post_name % c).group_by(Post.publish_time).having(Post.publish_time >= '2017-12-01', Post.publish_time <= '2018-01-31').order_by(Post.publish_time)

    labels_1 = []
    number_1 = []
    # labels_2 = []
    # number_2 = []
    # labels_3 = []
    # number_3 = []
    # labels_4= []
    # number_4 = []
    labels_5 = []
    number_5 = []

    for item in mydata:
        if item.publish_time >= '2017-12-01' and item.publish_time <= '2017-12-31':
            labels_1.append(item.publish_time)
            number_1.append(item.num)
        # elif item.publish_time >= '2018-1-01' and item.publish_time <= '2018-1-31':
        #     labels_2.append(item.publish_time)
        #     number_2.append(item.num)
        # elif item.publish_time >= '2018-2-01' and item.publish_time <= '2018-2-31':
        #     labels_3.append(item.publish_time)
        #     number_3.append(item.num)
        # elif item.publish_time >= '2018-3-01' and item.publish_time <= '2018-3-31':
        #     labels_4.append(item.publish_time)
        #     number_4.append(item.num)
        else:
            labels_5.append(item.publish_time)
            number_5.append(item.num)

    # print(labels_1)
    # print(number_1)
    # print(labels_2)
    # print(number_2)

    line = Line('大数据职位一个月内发布情况', title_text_size=25, title_pos="center", width=800, height=500)
    line.add("2017年12月", labels_1, number_1, is_label_show=True, label_text_size=35, legend_text_size=35, legend_orient='vertical', legend_pos='right', levisual_text_color="#fff")
    # line.add("2018年1月", labels_2, number_2, is_label_show=True, label_text_size=15, legend_text_size=15, legend_orient='vertical', legend_pos='right', levisual_text_color="#fff")
    #
    # line.add("2018年2月", labels_3, number_3, is_label_show=True, label_text_size=15, legend_text_size=15, legend_orient='vertical', legend_pos='right', levisual_text_color="#fff")
    # line.add("2018年3月", labels_4, number_4, is_label_show=True, label_text_size=15, legend_text_size=15, legend_orient='vertical', legend_pos='right', levisual_text_color="#fff")
    line.add("2018年4月", labels_5, number_5, is_label_show=True, label_text_size=15, legend_text_size=15, legend_orient='vertical', legend_pos='right', levisual_text_color="#fff")

    line.show_config()
    line.render('./templates/pubtime_line.html')
    # plt.savefig("../static/img/salary.png")

if __name__ == '__main__':
    keywords = ['数据', '人工智能', '机器学习']
    #     word_cloud(keywords)
    pubtime(keywords)

